package com.onlinewebgames.tictactoe.config;

import java.io.File;
import java.io.FileOutputStream;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;


public class ConfigConstants {

	public final static String ERROR = "ERROR";
	
	public static final boolean DEBUGFLAG = true;

	
	public static boolean initialized = false;
	public static boolean fExternalStorageAvailable = false;
	public static boolean fExternalStorageWritable = false;

	
	public static String APP_DIR;
	public static String MEDIA_CONTENT;
	
	//base de datos y tablas
	public final static String nombreTablaPartida = "Partida";
	public final static String	nombreTablaUser	= "User";
	public final static String	nombreBBDD	= "OnlineWebGames";
	////////////////////////
	
	
	//estados de la partida
	
	public final static String no_Aceptada = "no Aceptada";
	public final static String mueve_Jugador = "mueve Jugador";
	public final static String mueve_Contrario = "mueve Contrario";
	public final static String finalizada = "finalizada";
	
	////////////////////////
	
	
	//tipos de partida
	
	public final static String Tres_en_Raya = "Tres en Raya";
	
	////////////////////////
	
	
	public static boolean initializeSD(final Context ctx)
	{
		boolean result = false;

		if (checkExternalStorageState() && checkExternalStorageSpace())
		{
			try
			{
				final File mainDir = new File(ConfigConstants.APP_DIR);
				if(!mainDir.exists())
				{
					mainDir.mkdirs();
				}

				final File mediaDir = new File(ConfigConstants.MEDIA_CONTENT);
				if(!mediaDir.exists())
				{
					mediaDir.mkdirs();
				}

				// To prevent Android's photo gallery from getting images from this folder
				new FileOutputStream(new File(ConfigConstants.APP_DIR + ".nomedia"), true);
				new FileOutputStream(new File(ConfigConstants.MEDIA_CONTENT + ".nomedia"), true);

				result = true;
			}
			catch (final Exception e)
			{
				result = false;
				Log.e(ConfigConstants.ERROR, e.getMessage());//e.printStackTrace()
			}
		}
		return result;

	}
	
	public static boolean checkExternalStorageState()
	{
		final String state = Environment.getExternalStorageState();
	    if (Environment.MEDIA_MOUNTED.equals(state))
	    {
	        fExternalStorageAvailable = fExternalStorageWritable = true;
	    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
	    {
	        fExternalStorageAvailable = true;
	        fExternalStorageWritable = false;
	    } else
	    {
	        fExternalStorageAvailable = fExternalStorageWritable = false;
	    }
	    return (fExternalStorageAvailable && fExternalStorageWritable);
	}


	/**
	 * checkExternalStorageSpace
	 *
	 * Check external storage space
	 *
	 * @return true if SD space is greater than 100 MB, false in otherwise
	 */
	public static boolean checkExternalStorageSpace()
	{
		final StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
		final double sdAvailSize = (double)stat.getAvailableBlocks() *(double)stat.getBlockSize();
		//One binary gigabyte equals 1,073,741,824 bytes.
		final double gigaAvailable = sdAvailSize / 1073741824;
		return (gigaAvailable>(1/10));
	}

	
	
	
}
